/*******************************************************************************
 * Copyright (c) 2005, 2014 springside.github.io
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package com.yihu.wlyy.repository;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.yihu.wlyy.entity.patient.Patient;

import java.util.List;

public interface PatientDao extends PagingAndSortingRepository<Patient, Long> {

	// 查询患者姓名
	@Query("select p.name from Patient p where p.code=?1 and p.status=1")
	String findNameByCode(String code);

	// 根據患者標識查詢患者信息
	@Query("select p from Patient p where p.code=?1 and p.status=1")
	Patient findByCode(String code);

	// 根據身份證號查詢患者信息
	@Query("select p from Patient p where p.idcard=?1 and p.status=1")
	Patient findByIdcard(String idcard);
	
	@Query("select p from Patient p where p.ssc=?1 and p.status=1")
	Patient findBySsc(String ssc);

	// 根據手机号查詢患者信息
	@Query("select p from Patient p where p.mobile=?1 and p.status=1")
	Patient findByMobile(String mobile);

	// 根據病情等级获取患者信息
	@Query("select p from Patient p where p.diseaseCondition=?1 and p.status=1")
	Iterable<Patient> findInfoByLevel(int level);

	@Query("select p from Patient p where p.openid=?1 and p.status=1")
	Patient findByOpenid(String openid);
	
	@Query("select count(1) from Patient p where p.openid=?1 and p.status=1")
	int countByOpenid(String openid);
	
	//清空openid
	@Modifying
	@Query("update Patient p set p.openid = null where p.code <> ?1 and p.openid = ?2 and p.status=1")
	int clearOpenid(String patient, String openid);

	@Query(" select p from Patient p,SignFamily s where p.code=s.patient and s.status > 0 ")
	List<Patient> findAllSignPatient();
}
